
#include <errno.h>
#include <string.h>

#include "network/AbstractSocket.h"

extern int errno;

AbstractSocket::AbstractSocket()
    : iError_(-1), isOpen_(false)
{
}

AbstractSocket::~AbstractSocket()
{
    // Fermeture de la socket
    if(isOpen_)
        close();
}

bool AbstractSocket::close()
{
    if(!isOpen_)
        return false;

    if(::close(iSocket_) < 0)
    {
        iError_ = errno;
        return false;
    }

    isOpen_ = false;
    return true;
}

std::string AbstractSocket::errorString() const
{
    return std::string(strerror(iError_));
}

bool AbstractSocket::isOpen() const
{
    return isOpen_;
}

std::string AbstractSocket::interface() const
{
    return std::string(inet_ntoa(sin_.sin_addr));
}

int AbstractSocket::socketError() const
{
    return iError_;
}
